//
//  TeamAddViewController.m
//  Nschedule
//
//  Created by 이동민 on 13. 2. 15..
//  Copyright (c) 2013년 이동민. All rights reserved.
//

#import "TeamAddViewController.h"


@interface TeamAddViewController ()

@end

@implementation TeamAddViewController
@synthesize delegate = _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    _NameText.delegate=self;
    
    //키보드가 나오는 노티피케이션 등록
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow) name:
     UIKeyboardDidShowNotification object:nil];
    
    //키보드가 사라지는 노티피케이션 등록
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardDidHideNotification object:nil];
    
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}


//키보드 리턴키를 터치했을때
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //키패드가 화면에서 사라지게 됩니다.
    [textField resignFirstResponder];
    return YES;
}

//뷰를 터치하면 호출되는 메서드
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //키패트가 화면에서 사라지게 됩니다.
    [_NameText resignFirstResponder];
}

//키보드가 나오는 노티피케이션 함수
-(void)keyboardWillShow
{
    //뷰 이동 애니메이션
    [UIView beginAnimations:@"Button_Animation" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationsEnabled:YES];
    
    //뷰,텍스트필드 이동
    _CalImage.frame = CGRectMake(14, 96-87, _CalImage.frame.size.width, _CalImage.frame.size.height);
    _NameText.frame = CGRectMake(59, 146-87, _NameText.frame.size.width, _NameText.frame.size.height);
    _CancelButton.frame = CGRectMake(59, 248-87, _CancelButton.frame.size.width, _CancelButton.frame.size.height);
    _MakeButton.frame = CGRectMake(168, 248-87, _MakeButton.frame.size.width, _MakeButton.frame.size.height);
    _Member1.frame = CGRectMake(59, 178-87, _Member1.frame.size.width, _Member1.frame.size.height);
    _Member2.frame = CGRectMake(130, 178-87, _Member1.frame.size.width, _Member1.frame.size.height);
    _Member3.frame = CGRectMake(200, 178-87, _Member1.frame.size.width, _Member1.frame.size.height);
    _Member4.frame = CGRectMake(59, 214-87, _Member1.frame.size.width, _Member1.frame.size.height);
    _Member5.frame = CGRectMake(130, 214-87, _Member1.frame.size.width, _Member1.frame.size.height);
    _Member6.frame = CGRectMake(200, 214-87, _Member1.frame.size.width, _Member1.frame.size.height);

    //애니메이션 시작
    [UIView commitAnimations];
    
}
//키보드가 사라지는 노티피케이션 함수
-(void)keyboardWillHide
{
    //뷰 이동 애니메이션
    [UIView beginAnimations:@"Button_Animation" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationsEnabled:YES];
    
    //뷰,텍스트필드 이동
    _CalImage.frame = CGRectMake(14, 96, _CalImage.frame.size.width, _CalImage.frame.size.height);
    _NameText.frame = CGRectMake(59, 146, _NameText.frame.size.width, _NameText.frame.size.height);
    _CancelButton.frame = CGRectMake(59, 248, _CancelButton.frame.size.width, _CancelButton.frame.size.height);
    _MakeButton.frame = CGRectMake(168, 248, _MakeButton.frame.size.width, _MakeButton.frame.size.height);
    _Member1.frame = CGRectMake(59, 178, _Member1.frame.size.width, _Member1.frame.size.height);
    _Member2.frame = CGRectMake(130, 178, _Member1.frame.size.width, _Member1.frame.size.height);
    _Member3.frame = CGRectMake(200, 178, _Member1.frame.size.width, _Member1.frame.size.height);
    _Member4.frame = CGRectMake(59, 214, _Member1.frame.size.width, _Member1.frame.size.height);
    _Member5.frame = CGRectMake(130, 214, _Member1.frame.size.width, _Member1.frame.size.height);
    _Member6.frame = CGRectMake(200, 214, _Member1.frame.size.width, _Member1.frame.size.height);
    
    //애니메이션 시작
    [UIView commitAnimations];
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)CancelButton:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}




- (IBAction)MakeButton:(id)sender;
{
    [_delegate protocolTest:self.NameText.text];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}



//member1 버튼을 눌렀을때
- (IBAction)Member1:(id)sender
{
    
    dialog1 = [[UIAlertView alloc] initWithTitle:@"Member1"
                                                     message:@"멤버의 메일을 입력하세요.\n\n\n" // AlertView사이즈를 결정.
                                                    delegate:self
                                           cancelButtonTitle:@"Cancel"
                                           otherButtonTitles:@"Add", nil];
    tf=[[UITextField alloc]initWithFrame:CGRectMake(12.0, 80.0, 260, 25)];
    
    [tf setBackgroundColor:[UIColor whiteColor]];
    [dialog1 addSubview:tf];
    [dialog1 show];
}

- (IBAction)Member2:(id)sender
{
    dialog2 = [[UIAlertView alloc] initWithTitle:@"Member2"
                                         message:@"멤버의 메일을 입력하세요.\n\n\n" // AlertView사이즈를 결정.
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                               otherButtonTitles:@"Add", nil];
    tf=[[UITextField alloc]initWithFrame:CGRectMake(12.0, 80.0, 260, 25)];
    
    [tf setBackgroundColor:[UIColor whiteColor]];
    [dialog2 addSubview:tf];
    [dialog2 show];
}

- (IBAction)Member3:(id)sender
{
    dialog3 = [[UIAlertView alloc] initWithTitle:@"Member3"
                                         message:@"멤버의 메일을 입력하세요.\n\n\n" // AlertView사이즈를 결정.
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                               otherButtonTitles:@"Add", nil];
    tf=[[UITextField alloc]initWithFrame:CGRectMake(12.0, 80.0, 260, 25)];
    
    [tf setBackgroundColor:[UIColor whiteColor]];
    [dialog3 addSubview:tf];
    [dialog3 show];
}
- (IBAction)Member4:(id)sender
{
    dialog4 = [[UIAlertView alloc] initWithTitle:@"Member4"
                                         message:@"멤버의 메일을 입력하세요.\n\n\n" // AlertView사이즈를 결정.
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                               otherButtonTitles:@"Add", nil];
    tf=[[UITextField alloc]initWithFrame:CGRectMake(12.0, 80.0, 260, 25)];
    
    [tf setBackgroundColor:[UIColor whiteColor]];
    [dialog4 addSubview:tf];
    [dialog4 show];
}
- (IBAction)Member5:(id)sender
{
    dialog5 = [[UIAlertView alloc] initWithTitle:@"Member5"
                                         message:@"멤버의 메일을 입력하세요.\n\n\n" // AlertView사이즈를 결정.
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                               otherButtonTitles:@"Add", nil];
    tf=[[UITextField alloc]initWithFrame:CGRectMake(12.0, 80.0, 260, 25)];
    
    [tf setBackgroundColor:[UIColor whiteColor]];
    [dialog5 addSubview:tf];
    [dialog5 show];
}
- (IBAction)Member6:(id)sender
{
    dialog6 = [[UIAlertView alloc] initWithTitle:@"Member6"
                                         message:@"멤버의 메일을 입력하세요.\n\n\n" // AlertView사이즈를 결정.
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                               otherButtonTitles:@"Add", nil];
    tf=[[UITextField alloc]initWithFrame:CGRectMake(12.0, 80.0, 260, 25)];
    
    [tf setBackgroundColor:[UIColor whiteColor]];
    [dialog6 addSubview:tf];
    [dialog6 show];
}



//UIAlertView의 델리게이트 메서드 - 버튼을 눌러서 대화상자를 닫으면 호출되는 메서드
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    
    if(alertView == dialog1) //alertView가 dialog aletView라면..
    {
        if(buttonIndex == 1)
            [_Member1 setTitle: tf.text forState: UIControlStateNormal];
    }
    else if(alertView == dialog2)
    {
        if(buttonIndex == 1)
            [_Member2 setTitle: tf.text forState: UIControlStateNormal];
    }
    else if(alertView == dialog3)
    {
        if(buttonIndex == 1)
            [_Member3 setTitle: tf.text forState: UIControlStateNormal];
    }
    else if(alertView == dialog4)
    {
        if(buttonIndex == 1)
            [_Member4 setTitle: tf.text forState: UIControlStateNormal];
    }
    else if(alertView == dialog5)
    {
        if(buttonIndex == 1)
            [_Member5 setTitle: tf.text forState: UIControlStateNormal];
    }
    else if(alertView == dialog6)
    {
        if(buttonIndex == 1)
            [_Member6 setTitle: tf.text forState: UIControlStateNormal];
    }
    
}



@end
